package com.swh.recruit.controller;

import java.util.Date;
import java.util.Map;

import com.swh.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.swh.recruit.entity.TbRecruit;
import com.swh.recruit.service.TbRecruitService;

import com.swh.base.entity.Result;
import com.swh.base.entity.StatusCode;

/**
 * @author swh
 * @Description 职位 控制器层
 * @date 2021-01-12 17:57:44
 */
@RestController
@CrossOrigin
@RequestMapping("/tbRecruit")
public class TbRecruitController {

    @Autowired
    private TbRecruitService tbRecruitService;

    @Autowired
    IdUtil idUtil;


    /**
     * 查询状态不为 0 并以创建日期降序排序，查询前 12 条记录
     */
    @RequestMapping(value = "/search/newlist", method = RequestMethod.GET)
    public Result newlist() {
        return new Result(StatusCode.OK, "查询成功", tbRecruitService.findTop14ByStateOrderByCreatetimeDesc("0"));
    }

    /**
     * 查询状态为 2 并以创建日期降序排序，查询前 4 条记录
     */
    @RequestMapping(value = "/search/recommend", method = RequestMethod.GET)
    public Result findTop4ByStateOrderByCreatetimeDesc() {
        return new Result(StatusCode.OK, "查询热门公司", tbRecruitService.findTop4ByStateOrderByCreatetimeDesc("2"));
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, "查询成功", tbRecruitService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, "查询成功", tbRecruitService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param map  查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findPage(@RequestParam Map map, @PathVariable Integer page, @PathVariable Integer size) {
        Page<TbRecruit> page1 = tbRecruitService.findSearch(map, page, size);
        return new Result(StatusCode.OK, "带条件查询", page1);
    }


    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap) {
        return new Result(StatusCode.OK, "查询成功",
                tbRecruitService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param tbRecruit
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(TbRecruit tbRecruit) {
        tbRecruit.setId(idUtil.nextId());
        tbRecruit.setState("0");
        tbRecruit.setCreatetime(new Date());
        tbRecruitService.add(tbRecruit);
        return new Result(StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param tbRecruit
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(TbRecruit tbRecruit, @PathVariable String id) {
        tbRecruit.setId(id);
        tbRecruitService.update(tbRecruit);
        return new Result(StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        tbRecruitService.deleteById(id);
        return new Result(StatusCode.OK, "删除成功");
    }

}
